package br.ufc.si.managedBean;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import modelo.Imovel;
import modelo.Servico;
import modelo.Usuario;
import dao.ServicoDao;

@ManagedBean
public class CadastroServicoBean {
	private Servico servico;
	
	public CadastroServicoBean(){
		this.servico = new Servico();
	}


	private String msg;

	public boolean getMostraTabela() {
		ServicoDao servicoDao = new ServicoDao();
		return servicoDao.getLista().size() > 0;
	}

	public String cadastrar() throws IOException, ClassNotFoundException {

		ServicoDao servicoDao = new ServicoDao();
		try {
			servicoDao.save(servico);
			servico = new Servico();

			msg = "Serviço cadastrado com sucesso";

			return "/admin/index_admin";

		} catch (PersistenceException ex) {
			ex.printStackTrace();
			msg = "Serviço já cadastrado!";

			return "/locador/indexLocador?faces-redirect=true";
		}

	}

	public String atualizar() throws ClassNotFoundException {

		ServicoDao servicoDao = new ServicoDao();
		try{
			servicoDao.update(servico);
			msg = "Serviço atualizado";
			
			return "/admin/index_admin";
			
		}catch(PersistenceException ex){
			ex.printStackTrace();
			msg = "Serviço não atualizado atualizado";			
			return "/admin/index_admin";
		}

	}

	public String visualizar() {
		ServicoDao servicoDao = new ServicoDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			servico = servicoDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return "/admin/servicos/vizualizar_servico";
	}

	public String editar() {

		ServicoDao servicoDao = new ServicoDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			servico = servicoDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return "/admin/servicos/editar_servico";
	}

	public String excluir() {
		ServicoDao servicoDao = new ServicoDao();

		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		servicoDao.find(Integer.parseInt(request.getParameter("id")));

		return "/admin/index_admin?faces-redirect=true";
	}

	public String paginaCadastroServico() {

		return "/admin/servicos/cadastrar_servicos?faces-redirect=true";
	}

	public String cancelar() {
		return "/admin/index_admin";
	}

	public Servico getServico() {
		return servico;
	}

	public void setServico(Servico servico) {
		this.servico = servico;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ServicoDao getServicoDao(){
		return new ServicoDao();
	}
	
}
